package logic

import (
	"context"
	"fmt"
	"recruit/model"

	"recruit/recruitrpc/internal/svc"
	"recruit/recruitrpc/recruitrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *recruitrpc.LoginRequest) (*recruitrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var user model.User
	model.DB.Where("mobile", in.Mobile).Find(&user)
	if user.ID != 0 {
		if user.Password != in.Password {
			return nil, fmt.Errorf("密码错误")
		}
	} else {
		user = model.User{
			Mobile:   in.Mobile,
			Password: in.Password,
		}
		model.DB.Create(&user)
	}
	return &recruitrpc.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
